## setup

# clean environment
rm(list=ls())

# load packages
library(ggplot2)
library(plotrix)
library(dplyr)


# load data
dat <- read.csv2("data_survey_csv.csv",
                 encoding = 'UTF-8')


## prepare data for figure

# contact/accessibility with/of legislators
contact_df <- data.frame(mean=c(mean(dat$contact_state), mean(dat$contact_fed), mean(dat$access_state), mean(dat$access_fed)),
                         se=c(std.error(dat$contact_state), std.error(dat$contact_fed), std.error(dat$access_state), std.error(dat$access_fed)),
                         var=c("Contact", "Contact", "Access", "Access"),
                         level=c("State", "Federal", "State", "Federal"))


## create figure

ggplot(contact_df, aes(y=mean, x=var, group=factor(level), fill=factor(level))) + 
  geom_bar(stat="identity", position="dodge") +
  geom_text(aes(label=round(mean, 2)), position = position_dodge(width = .9), vjust=-1.5, size=3) +
  geom_errorbar(aes(ymin=mean-se, ymax=mean+se), width=.2, position=position_dodge(width=.9)) +
  scale_y_continuous(limits=c(0,7), breaks=seq(1, 7, 1)) +
  coord_cartesian(ylim=c(1,7), expand=F) +
  xlab("") + ylab("Mean") +
  scale_fill_manual(values=c("lightblue", "red"), name="MPs") +
  theme_bw() +
  theme(panel.grid = element_blank(), legend.position = "bottom", text = element_text(size=14))
